package com.service.database.handler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.service.database.model.DataColumn;
import com.service.database.utils.CommonUtils;
import com.service.database.config.DataBase;
import com.service.database.constants.SqlConstants;

import java.util.ArrayList;
import java.util.List;

/**
 * 达梦
 * @author xianggj
 * @Date 2021/6/4 22:17
 **/
public class Dm8Handler extends AbstractDatabaseHandler{
    public Dm8Handler(DataBase type) {
        super(type);
    }

    @Override
    public String urlSpecialDeal(StringBuffer currentUrl) {
        return currentUrl.toString();
    }

    @Override
    public String getShowDataBaseSql() {
        return String.format("select object_name as %s from dba_objects", returnKeys);
    }

    @Override
    public String getShowTablesSql() {
        return String.format("select table_name as %s,comments as %s from user_tab_comments where table_type='TABLE'", returnKeys);
    }

    @Override
    public String getShowTableDescSql() {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT ");
        sql.append(String.format("utc.COLUMN_NAME AS %s," +
                "utc.DATA_TYPE AS %s," +
                "utc.NULLABLE AS %s," +
                "CASE WHEN cu.COLUMN_NAME = utc.COLUMN_NAME THEN 'YES' ELSE 'NO' END AS %s," +
                "utc.DATA_LENGTH AS %s," +
                "CASE WHEN DATA_SCALE != 0 THEN DATA_PRECISION || ',' || DATA_SCALE ELSE DATA_PRECISION || '' END %s," +
                "t.COMMENTS AS %s ",returnKeys));
        sql.append("FROM user_tab_columns utc " );
        sql.append("LEFT JOIN user_col_comments t ON t.COLUMN_NAME = utc.COLUMN_NAME " +
                "AND utc.TABLE_NAME = t.TABLE_NAME ");
        sql.append("LEFT JOIN ( SELECT cu.table_name AS table_name, " +
                "cu.COLUMN_NAME AS COLUMN_NAME  FROM " +
                "user_cons_columns cu,user_constraints au  " +
                "WHERE cu.constraint_name = au.constraint_name " +
                "AND au.constraint_type = 'P') cu ON utc.TABLE_NAME = cu.TABLE_NAME " +
                "AND utc.COLUMN_NAME = cu.COLUMN_NAME " );
        sql.append("WHERE utc.TABLE_NAME =? ");
        return sql.toString();
    }


    @Override
    public String getShowTableDataSql() {
        return String.format("select * from %s ",  addChar(tableName));
    }

    @Override
    public String getShowTableDataNumSql() {
        return String.format("select count(0) as %s from %s", returnKeys[0], addChar(tableName));
    }

    @Override
    public String distinctColSql() {
        StringBuffer sql = new StringBuffer("select distinct ");
        List<String> sqls = new ArrayList<>();
        for (int i = 0; i < cols.length; i++) {
            if (returnKeys.length > i){
                sqls.add(String.format(" %s as %s", cols[i], returnKeys[i]));
            } else {
                sqls.add(String.format(" %s", cols[i]));
            }
        }
        sql.append(CollUtil.join(sqls, SqlConstants.COMMA));
        sql.append(String.format(" from %s ", addChar(tableName)));
        return sql.toString();
    }

    @Override
    public String addLimitBySql(String sql, Integer num, Integer offset) {
        String outSql = CommonUtils.substringNotBracketsString(sql);
        //如果包含limit
        if (StrUtil.containsIgnoreCase(outSql, SqlConstants.LIMIT)){
            sql = String.format(SqlConstants.SELECT_CONTAIN, sql);
        }
        if (num == null){
            return sql;
        }
        sql += SqlConstants.LIMIT + num;
        if (offset != null){
            sql += SqlConstants.OFFSET + offset;
        }
        return  sql;
    }

    @Override
    public String dropTableSql() {
        return String.format("DROP TABLE IF EXISTS %s.%s ",
                addChar(dbName), addChar(tableName));
    }

    @Override
    public List<String> createTableSql(List<DataColumn> columns) {
        throw new RuntimeException("还没实现");
    }

    @Override
    protected String intToType(int i) {
        return null;
    }
}
